//
//  LSZDynamicDetailsClassificationVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/8/23.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZDynamicDetailsClassificationVC.h"
#import "LCUtils.h"

@interface LSZDynamicDetailsClassificationVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableViewDetailsClass;
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,strong) NSMutableArray * mutArrayData;
@property(nonatomic,strong) LSZEmptyDataVC * emptyDataVC;
@end

@implementation LSZDynamicDetailsClassificationVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.emptyDataVC removeFromSuperview];
    [self getCityData];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.emptyDataVC removeFromSuperview];
}
#pragma mark - 得到同城信息
-(void)getCityData
{
    [self addMoreData];
    self.page = 1;
    [self initWithCityDataWithPage:self.page];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTitle];
}
#pragma mark - 数据请求
-(void)initWithCityDataWithPage:(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    NSString * string = NullStringjudgment(NSStr(self.hot));
    if (string.length == 0) {
        self.hot = @"";
    }
    [LSZRequestResolutionDic allDynamicLists:self.kind id_id:@"" type:@"" fuid:@"" selfStr:@"" theme:NullStringjudgment(NSStr(self.theme)) page:[NSString stringWithFormat:@"%ld",(long)page] is_city:@"0" is_follow:@"" hot:self.hot :^(BOOL isOK, NSString * string, LSZAllDynamicListsManager * manager) {
        if (isOK == YES) {
            if (page == 1) {
                [weakSelf.mutArrayData  removeAllObjects];
                [weakSelf.mutArrayData addObjectsFromArray:manager.arrayData];
                if (manager.arrayData.count == 0) {
                    weakSelf.tableViewDetailsClass.mj_footer.hidden = YES;
                    weakSelf.emptyDataVC = [[LSZEmptyDataVC alloc]init];
                    [weakSelf.emptyDataVC showWithView:weakSelf.tableViewDetailsClass];
                }
            }else{
                [weakSelf.mutArrayData addObjectsFromArray:manager.arrayData];
                if (manager.arrayData.count == 0) {
                    [weakSelf.tableViewDetailsClass.mj_footer endRefreshingWithNoMoreData];
                }
            }
            [self.tableViewDetailsClass reloadData];
        }else{
            [self initWithtring:string];
        }
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZAllDynamicListsMg * mg = nil;
    mg = self.mutArrayData[indexPath.section];
    if(indexPath.row == 0){
        float A = [LSZSpecialPackaging initWithReturnsthesizeOf:13.5 :0.5];
        int C;
        if (ScreenWidth == 320) {
            C = 70;
        }else if(ScreenWidth == 375){
            C = 77;
        }else {
            C = 85;
        }
        NSString * string = [NSString stringWithFormat:@"%@%@",mg.theme_content,mg.content];
        CGFloat sizeImage = [LSZSpecialPackaging initWithReturnsthesizeOf:45 :5];
        CGFloat height;
        if ([string isEqualToString:@""]) {
            height = 0;
        }else{
            height = [LSZSpecialPackaging caculateHeightOfText:ScreenWidth-sizeImage-25 height:80 font:A :string];
        }
        return  height-10+C;
    }else if(indexPath.row == 1){
        CGFloat heightA = [LSZSpecialPackaging initWithReturnsthesizeOf:60 :5];

        CGFloat wids = (ScreenWidth-30-heightA)/3;
        if(mg.arrayImages.count == 0){
            return  0.00001;
        }
        else if (mg.arrayImages.count == 1) {
            return (ScreenWidth-30-heightA)/2;
        }else{
            if(mg.arrayImages.count%3 == 0){
                return (mg.arrayImages.count/3)*(wids+10);
            }else{
                return (mg.arrayImages.count/3+1)*(wids+10);
            }
        }
    }else if (indexPath.row == 2){
        if (mg.resources_name.length ==0) {
            return 0.0001;
        }else{
            return [LSZSpecialPackaging initWithReturnsthesizeOf:50 :5];
        }
    }
    return 80;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  self.mutArrayData.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.mutArrayData.count > indexPath.section) {
        LSZAllDynamicListsMg * mg = nil;
        mg = self.mutArrayData[indexPath.section];
        if (indexPath.row == 0) {
            LSZBasicInformationCollectionViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZBasicInformationCollectionViewCell" forIndexPath:indexPath];
            cell.AllDynamicListsMg = mg;
            [cell.butFocusOn tapGesture:^(UIGestureRecognizer *ges) {
                [self clickButFocusOnDetaill:mg.uid :indexPath.section :mg];
            }];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            return cell;
        }else if (indexPath.row == 1){
            LSZImageViewFocusOnTableViewCell * cell = [[LSZImageViewFocusOnTableViewCell alloc]initWithImageViewFocusOn:mg.arrayImages];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            return cell;
        }else if (indexPath.row == 2){
            if (mg.resources_name.length ==0) {
                UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
                if (!cell) {
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone ;
                return cell;
            }
            LSZAddMusicCollectionViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZAddMusicCollectionViewCell" forIndexPath:indexPath];
            cell.AllDynamicListsMg = mg;
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            return cell;
        }
        else{
            LSZThumbUpForwardCollectionViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZThumbUpForwardCollectionViewCell" forIndexPath:indexPath];
            cell.AllDynamicListsMg = mg;
            [cell.imageViewEvaluation tapGesture:^(UIGestureRecognizer *ges) {
                [self goToDynamicReview:mg];
            }];
            [cell.labelEvaluation tapGesture:^(UIGestureRecognizer *ges) {
                [self goToDynamicReview:mg];
            }];
            [cell.imageViewShare tapGesture:^(UIGestureRecognizer *ges) {
                [self labelShareClickWithMg:mg section:indexPath.section];
            }];
            [cell.labelShare tapGesture:^(UIGestureRecognizer *ges) {
                [self labelShareClickWithMg:mg section:indexPath.section];
            }];
            [cell.imageViewZan tapGesture:^(UIGestureRecognizer *ges) {
                [self initWiththeDynamicPointOfPraise:mg :indexPath.section];
            }];
            [cell.labelZan tapGesture:^(UIGestureRecognizer *ges) {
                [self initWiththeDynamicPointOfPraise:mg :indexPath.section];
            }];
            [cell.butMore tapGesture:^(UIGestureRecognizer *ges) {
                [self chooseToReport:mg];
            }];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            return cell;
        }
    }else{
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone ;
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0||indexPath.row == 1) {
        LSZAllDynamicListsMg * mg = nil;
        mg = self.mutArrayData[indexPath.section];
        LSZDynamicCommentDetailsVC * vc = [[LSZDynamicCommentDetailsVC alloc]init];
        vc.AllDynamicListsMg = mg;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else  if (indexPath.row == 2) {
        LSZAllDynamicListsMg * mg = nil;
        mg = self.mutArrayData[indexPath.section];
        NSString * str = [[NSString alloc]init];
        NSString * id_id_str = [[NSString alloc]init];
        if ([mg.type isEqualToString:@"2"]) {
            str = @"/Music/get_url";
        }else if ([mg.type isEqualToString:@"5"]){
            str = @"/Music/artist";
        }else{
            str = @"/Music/album";
        }
        id_id_str = mg.resources_id;
        [LSZRequestResolutionDic dynamicGetsAlbumArtistSongDataBasedOn:id_id_str URL:str type:mg.type :^(BOOL isOk, NSString * string, LSZNewAudioHomepageManager * manager) {
            if (isOk == YES) {
                if ([mg.type isEqualToString:@"2"]) {
                    [LCUtils lcListPlayWithLSZNewAudioHomepageManager:manager];
                }else{
                    LSZNewAudioPlayImageAndTableViewVC * vc = [[LSZNewAudioPlayImageAndTableViewVC alloc]init];
                    LSZNewAudioHomepageMg * audioMg = nil;
                    audioMg = manager.arrayData[0];
                    [LCUtils albumArtistDetailsInterfaceJump:vc audioMg:audioMg typeComeWhere:@"1"];
                    vc.lastPageNumberRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
                    vc.lastSection = [NSString stringWithFormat:@"%ld",(long)indexPath.section-1];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
        }];
    }
}
#pragma mark - 进入动态评论
-(void)goToDynamicReview :(LSZAllDynamicListsMg *)mg
{
    LSZDynamicCommentDetailsVC * vc = [[LSZDynamicCommentDetailsVC alloc]init];
    vc.AllDynamicListsMg = mg;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 对动态点赞
-(void)initWiththeDynamicPointOfPraise :(LSZAllDynamicListsMg *)mg :(NSInteger)section
{
    if ([[_UserM usercode] isEqualToString:@""]) {
        pushAccLoginVC;
        return;
    }
    NSString * ck = [[NSString alloc]init];
    NSString * mode = [[NSString alloc]init];
    if ([mg.is_admire isEqualToString:@"0"]) {
        ck = @"0";//点赞
        mode = @"";
    }else{
        ck = @"";//取消点赞
        mode = @"1";
    }
    [LSZRequestData theDynamicPointOfPraise:mg.id_id type:@"1" ck:ck mode:mode :^(BOOL isOk, NSString * string, NSDictionary * dic) {
        if (isOk == YES) {
            if([ck isEqualToString:@"0"]){
                mg.is_admire = @"1";
                mg.admire_num = [NSString stringWithFormat:@"%ld",(long)([mg.admire_num integerValue]+1)];
                [self initWithtring:@"点赞成功!"];
            }else{
                mg.is_admire = @"0";
                mg.admire_num = [NSString stringWithFormat:@"%ld",(long)([mg.admire_num integerValue]-1)];
                [self initWithtring:@"取消点赞!"];
            }
            LSZThumbUpForwardCollectionViewCell * cell = (LSZThumbUpForwardCollectionViewCell *)[self.tableViewDetailsClass cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3  inSection:section]];
            if([mode isEqualToString:@"1"]){
                cell.imageViewZan.image = [UIImage imageNamed:@"点赞"];

            }else{
                cell.imageViewZan.image = [UIImage imageNamed:@"我的_签到_留言_已点赞"];
            }
            cell.labelZan.text = mg.admire_num;
        }else{
            [self initWithtring:string];
        }
    }];
}
#pragma mark - 关注
-(void)clickButFocusOnDetaill :(NSString *)uid :(NSInteger)section :(LSZAllDynamicListsMg *)mg
{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
    }else{
        [LSZRequestData payAttentionToOthers:uid Ck:@"0" mode:@"0" :^(BOOL isOk, NSString * str, NSDictionary * dic) {
            if (isOk == YES) {
                [self initWithtring:@"关注成功!"];
                LSZBasicInformationCollectionViewCell * cell = (LSZBasicInformationCollectionViewCell *)[self.tableViewDetailsClass cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0  inSection:section]];
                [cell.butFocusOn setTitle:@"已关注" forState:UIControlStateNormal];
                cell.butFocusOn.backgroundColor =  partialShallow;
                cell.butFocusOn.userInteractionEnabled = NO;
            }else{
                [self initWithtring:str];
            }
        }];
    }
}
-(UITableView *)tableViewDetailsClass
{
    if (_tableViewDetailsClass == nil) {
        _tableViewDetailsClass = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-44-HOME_INDICATOR_HEIGHT-NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped];
        _tableViewDetailsClass.delegate = self;
        _tableViewDetailsClass.dataSource = self;
        _tableViewDetailsClass.separatorColor = divider;
        _tableViewDetailsClass.estimatedRowHeight =0;
        _tableViewDetailsClass.estimatedSectionFooterHeight =0;
        _tableViewDetailsClass.estimatedSectionHeaderHeight =0;
        _tableViewDetailsClass.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        _tableViewDetailsClass.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableViewDetailsClass];
    }
    return _tableViewDetailsClass;
}
-(void)addMoreData
{
    [self.tableViewDetailsClass registerClass:[LSZBasicInformationCollectionViewCell class] forCellReuseIdentifier:@"LSZBasicInformationCollectionViewCell"];
    [self.tableViewDetailsClass registerClass:[LSZAddMusicCollectionViewCell class] forCellReuseIdentifier:@"LSZAddMusicCollectionViewCell"];
    [self.tableViewDetailsClass registerClass:[LSZThumbUpForwardCollectionViewCell class] forCellReuseIdentifier:@"LSZThumbUpForwardCollectionViewCell"];
    self.tableViewDetailsClass.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.page = 1;
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self initWithCityDataWithPage :self.page];
            [self.tableViewDetailsClass.mj_header endRefreshing];
        });
    }];
    // 上拉刷新
    self.tableViewDetailsClass.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self initWithCityDataWithPage:self.page];
            [self.tableViewDetailsClass.mj_footer endRefreshing];
        });
    }];
}
-(void)addTitle
{
    self.mutArrayData = [[NSMutableArray alloc]init];
}
#pragma mark - 送积分
- (void)labelShareClickWithMg:(LSZAllDynamicListsMg *)mg section:(NSInteger)section
{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
        return;
    }
    LCSendSugarDemo6 *sendSugar = [[LCSendSugarDemo6 alloc] init];
    sendSugar.mg = mg;
    CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 40, 360);
    sendSugar.frame = rect;
    [sendSugar.undoButton addTarget:self action:@selector(undoButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
    @weakify(self)
    [sendSugar.sugarListBut tapGesture:^(UIGestureRecognizer *ges) {
        @strongify(self)
        [self sugarListButClickWithMg:mg];
    }];
    sendSugar.successBlock = ^{
        @strongify(self)
        [self.zh_popupController dismiss];
    };
    self.zh_popupController = [zhPopupController new];
    self.zh_popupController.layoutType = zhPopupLayoutTypeCenter;
    [self.zh_popupController presentContentView:sendSugar duration:0.25 springAnimated:false];
}
- (void)undoButtonClick
{
    [self.zh_popupController dismiss];
}
- (void)sugarListButClickWithMg:(LSZAllDynamicListsMg *)mg
{
    [self.zh_popupController dismiss];
    LCSugarListTableViewController *viewController = [[LCSugarListTableViewController alloc] init];
    viewController.mg = mg;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        viewController.title = @"赠送列表";
    });
    [self.navigationController pushViewController:viewController animated:YES];
}
#pragma mark - 选择举报
-(void)chooseToReport:(LSZAllDynamicListsMg *)manager
{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
        return;
    }
    ACActionSheet *actionSheet = [[ACActionSheet alloc] initWithTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"举报"] actionSheetBlock:^(NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            LSZReportViewController * VC = [[LSZReportViewController alloc]init];
            VC.name = manager.username;
            VC.titleString = manager.content;
            VC.tid = manager.id_id;
            VC.type = @"1";
            VC.comment_id = @"";
            [self.navigationController pushViewController:VC animated:YES];
        }
    }];
    [actionSheet show];
}
@end
